package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.common.Result;
import com.demo.pojo.entity.Notice;
import com.demo.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 通知管理控制器
 * 支持管理员、教师发布通知，学生查看通知
 */
@RestController
@RequestMapping("/api")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    // ==================== 管理员接口 ====================

    /**
     * 管理员：获取通知列表（分页）
     */
    @GetMapping("/admin/notices")
    public Result<IPage<Notice>> getAdminNotices(
            @RequestParam(required = false) String publisherType,
            @RequestParam(required = false) Long publisherId,
            @RequestParam(required = false) String noticeType,
            @RequestParam(required = false) String semester,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        IPage<Notice> result = noticeService.getNoticeList(
                publisherType, publisherId, noticeType, semester, status, page, pageSize
        );
        return Result.success(result);
    }

    /**
     * 管理员：创建通知
     */
    @PostMapping("/admin/notice/create")
    public Result<Long> createAdminNotice(@RequestBody Notice notice) {
        Long noticeId = noticeService.createNotice(notice);
        return Result.success(noticeId);
    }

    /**
     * 管理员：更新通知
     */
    @PostMapping("/admin/notice/update")
    public Result<Boolean> updateAdminNotice(@RequestBody Notice notice) {
        boolean success = noticeService.updateNotice(notice);
        return Result.success(success);
    }

    /**
     * 管理员：删除通知
     */
    @DeleteMapping("/admin/notice/{noticeId}")
    public Result<Boolean> deleteAdminNotice(@PathVariable Long noticeId) {
        boolean success = noticeService.deleteNotice(noticeId);
        return Result.success(success);
    }

    /**
     * 管理员：获取班级列表
     */
    @GetMapping("/admin/clazz/list")
    public Result<List<Map<String, Object>>> getClazzList() {
        List<Map<String, Object>> list = noticeService.getClazzList();
        return Result.success(list);
    }

    /**
     * 管理员：获取学生列表
     */
    @GetMapping("/admin/student/list")
    public Result<List<Map<String, Object>>> getStudentList() {
        List<Map<String, Object>> list = noticeService.getStudentList();
        return Result.success(list);
    }

    // ==================== 教师接口 ====================

    /**
     * 教师：获取教授的班级列表
     */
    @GetMapping("/teacher/my-classes")
    public Result<List<Map<String, Object>>> getTeacherClasses(@RequestParam Long teacherId) {
        List<Map<String, Object>> list = noticeService.getTeacherClasses(teacherId);
        return Result.success(list);
    }

    /**
     * 教师：获取通知列表（分页）
     */
    @GetMapping("/teacher/notices")
    public Result<IPage<Notice>> getTeacherNotices(
            @RequestParam(required = false) String publisherType,
            @RequestParam(required = false) Long publisherId,
            @RequestParam(required = false) String noticeType,
            @RequestParam(required = false) String semester,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        IPage<Notice> result = noticeService.getNoticeList(
                publisherType, publisherId, noticeType, semester, 1, page, pageSize
        );
        return Result.success(result);
    }

    /**
     * 教师：创建通知
     */
    @PostMapping("/teacher/notice/create")
    public Result<Long> createTeacherNotice(@RequestBody Notice notice) {
        Long noticeId = noticeService.createNotice(notice);
        return Result.success(noticeId);
    }

    /**
     * 教师：更新通知
     */
    @PostMapping("/teacher/notice/update")
    public Result<Boolean> updateTeacherNotice(@RequestBody Notice notice) {
        boolean success = noticeService.updateNotice(notice);
        return Result.success(success);
    }

    /**
     * 教师：删除通知
     */
    @DeleteMapping("/teacher/notice/{noticeId}")
    public Result<Boolean> deleteTeacherNotice(@PathVariable Long noticeId) {
        boolean success = noticeService.deleteNotice(noticeId);
        return Result.success(success);
    }

    // ==================== 学生接口 ====================

    /**
     * 学生：获取通知列表
     */
    @GetMapping("/student/notices")
    public Result<List<Map<String, Object>>> getStudentNotices(
            @RequestParam Long studentId,
            @RequestParam(required = false) String semester
    ) {
        List<Map<String, Object>> list = noticeService.getStudentNotices(studentId, semester);
        return Result.success(list);
    }

    /**
     * 学生：标记通知为已读
     */
    @PostMapping("/student/notice/read")
    public Result<Boolean> markNoticeRead(@RequestBody Map<String, Object> params) {
        Long studentId = Long.valueOf(params.get("studentId").toString());
        Long noticeId = Long.valueOf(params.get("noticeId").toString());
        boolean success = noticeService.markNoticeRead(studentId, noticeId);
        return Result.success(success);
    }
}